var SettingHandler = function () {
}

SettingHandler.prototype.listenAvatarUploadEvent = function () {
    $('#avatar-input').on('change', function () {
        var file = this.files[0];
        var formData = new FormData()
        formData.append('image', file)
        zlajax.post({
            url: '/avatar/upload',
            data: formData,
            // 不转化字符串
            processData: false,
            // 不设置内容类型
            contentType: false,
            success: function (data) {
                if (data['code'] === 200) {
                    let avatar = data['data']['avatar']
                    let avatar_url = "/media/avatar/" + avatar
                    $('#avatar-img').attr('src', avatar_url)
                }
            }
        })
    })
}

SettingHandler.prototype.listenSubmitEvent = function () {
    $('#submit-btn').on('click', function (e) {
        e.preventDefault()
        let signature = $('#signagure-input').val()
        if (signature.length > 100) {
            alert('个性签名不能超过100个字')
        }
        zlajax.post({
            url: '/profile/edit',
            data: {
                'signature': signature
            },
            success: function (data) {
                if (data['code'] === 200) {
                    alert('提交成功!')
                }else{
                    alert(data['message'])
                }
            }
        })
    })
}

SettingHandler.prototype.run = function () {
    this.listenAvatarUploadEvent()
    this.listenSubmitEvent()
}


$(function () {
    var handler = new SettingHandler();
    handler.run();
});